< previous page page_95 next page >

Page 95
Dim theCheckingAccount As New CheckingAccount
Dim theBankAccount As BankAccount
Set theBankAccount = theCheckingAccount
theBankAccount.deposit
What's the logic behind this? Although BankAccount might resemble an abstract class, it isn't one. It is a true interface class, meaning that although BankAccount's interface to its clients remains the same, its implementation is delegated to the inheriting class CheckingAccount, a fact that is seamless to the client. This facilitates a plug-and-play mechanism whereby you can remove an implementing class and replace it with another that also implements the same interface. This other class could be, say, SavingsAccount. SavingsAccount would then specialize the deposit method, but the BankAccount interface class, which is broadly known by many classes, remains untouched. This means that you're spared the chore of having to go to every section of code that references the BankAccount object.
Understanding Polymorphism
Because Visual Basic supports interface inheritance, polymorphism is supported through the hierarchy of classes that implement a common interface. Polymorphism literally means many forms. In technical terms, polymorphism is realized when many classes implement a class's interface in special, unique ways.
Borrowing from your interface inheritance example, assume you have a BankAccount interface class and two implementation classes, CheckingAccount and SavingsAccount. Because BankAccount has a public method, deposit, both classes must implement it. Further, let's assume that SavingsAccount and CheckingAccount implement deposit as follows:
'SavingsAccount
Private Sub deposit(argAmount As Single)
    msgl_Balance = msgl_Balance + argAmount
End Sub
'CheckingAccount
Private Sub deposit(argAmount As Single)
    msgl_Balance = msgl_Balance + argAmount - Fee
End Sub
The following will use the first implementation:
Dim theSavingsAccount As New SavingsAccount
Dim theBankAccount As BankAccount
Set theBankAccount = theSavingsAccount
theBankAccount.deposit

 
< previous page page_95 next page >

If you like this book, buy it!